/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include <kal.h>
/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_errno.h>

#include <k_mutex.h>
#include <k_sem.h>
#include <k_tick.h>
#include <k_task.h>

#ifdef CONFIG_IPC_EVENT
#include <k_event.h>
#endif

#ifdef CONFIG_IPC_MQ
#include <k_mq.h>
#endif

#ifdef CONFIG_SOFT_TIMER
#include <k_timer.h>
#endif

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

int k_task_create(k_task_t *task, const char *name, uint8_t prio, size_t stack_size,
                  int (*entry)(uintptr_t arg), uintptr_t arg)
{
    if (x_magic_verify(task) != 0) {
        return -1;
    }
    task_id t = task_spawn(name, prio, 0, stack_size, entry, arg);
    if (t == NULL) {
        return -1;
    }
    x_magic_set(task, t);
    return 0;
}

int k_task_delete(k_task_t *task)
{
    if (!x_magic_verify(task)) {
        return -1;
    }
    task_id t = (task_id)x_magic_get(task);
    if (t == NULL) {
        return -1;
    }
    if (task_delete(t) != 0) {
        return -1;
    }
    x_magic_reset(task);
    return 0;
}

int k_task_exit(void)
{
    task_exit();
    return -1;
}

int k_task_resume(k_task_t *task)
{
    if (!x_magic_verify(task)) {
        return -1;
    }
    task_id t = (task_id)x_magic_get(task);
    if (t == NULL) {
        return -1;
    }
    if (task_resume(t) != 0) {
        return -1;
    }
    return 0;
}

int k_task_suspend(k_task_t *task)
{
    if (!x_magic_verify(task)) {
        return -1;
    }
    task_id t = (task_id)x_magic_get(task);
    if (t == NULL) {
        return -1;
    }
    if (task_suspend(t) != 0) {
        return -1;
    }
    return 0;
}

void k_task_lock(void)
{
    task_lock();
}

void k_task_unlock(void)
{
    task_unlock();
}

void k_task_msleep(uint32_t ms)
{
    task_msleep(ms);
}

int k_task_prio_set(k_task_t *task, int prio)
{
    if (!x_magic_verify(task)) {
        return -1;
    }
    task_id t = (task_id)x_magic_get(task);
    if (t == NULL) {
        return -1;
    }
    if (task_prio_set(t, prio) != 0) {
        return -1;
    }
    return 0;
}

int k_task_prio_get(k_task_t *task)
{
    if (!x_magic_verify(task)) {
        return -1;
    }
    task_id t = (task_id)x_magic_get(task);
    if (t == NULL) {
        return -1;
    }
    return (int)task_prio_get(t);
}

void k_task_stat(void)
{
}

int k_task_stack_space(k_task_t *task)
{
    if (!x_magic_verify(task)) {
        return -1;
    }
    task_id t = (task_id)x_magic_get(task);
    if (t == NULL) {
        return -1;
    }
    return (int)task_stack_space(t);
}

int k_mutex_init(k_mutex_t *mutex)
{
    if (x_magic_verify(mutex) != 0) {
        return -1;
    }
    mutex_id m = mutex_create();
    if (m == NULL) {
        return -1;
    }
    x_magic_set(mutex, m);
    return 0;
}

int k_mutex_destroy(k_mutex_t *mutex)
{
    if (!x_magic_verify(mutex)) {
        return -1;
    }
    mutex_id m = (mutex_id)x_magic_get(mutex);
    if (m == NULL) {
        return -1;
    }
    if (mutex_destroy(m) != 0) {
        return -1;
    }
    x_magic_reset(mutex);
    return 0;
}

int k_mutex_timedlock(k_mutex_t *mutex, uint32_t timeout)
{
    if (!x_magic_verify(mutex)) {
        return -1;
    }
    mutex_id m = (mutex_id)x_magic_get(mutex);
    if (m == NULL) {
        return -1;
    }
    if (mutex_timedlock(m, timeout) != 0) {
        return -1;
    }
    return 0;
}

int k_mutex_trylock(k_mutex_t *mutex)
{
    if (!x_magic_verify(mutex)) {
        return -1;
    }
    mutex_id m = (mutex_id)x_magic_get(mutex);
    if (m == NULL) {
        return -1;
    }
    if (mutex_trylock(m) != 0) {
        return -1;
    }
    return 0;
}

int k_mutex_lock(k_mutex_t *mutex)
{
    if (!x_magic_verify(mutex)) {
        return -1;
    }
    mutex_id m = (mutex_id)x_magic_get(mutex);
    if (m == NULL) {
        return -1;
    }
    if (mutex_lock(m) != 0) {
        return -1;
    }
    return 0;
}

int k_mutex_unlock(k_mutex_t *mutex)
{
    if (!x_magic_verify(mutex)) {
        return -1;
    }
    mutex_id m = (mutex_id)x_magic_get(mutex);
    if (m == NULL) {
        return -1;
    }
    if (mutex_unlock(m) != 0) {
        return -1;
    }
    return 0;
}

int k_sem_init(k_sem_t *sem, int limit, int value)
{
    if (x_magic_verify(sem)) {
        return -1;
    }
    sem_id s = sem_create(value);
    if (s == NULL) {
        return -1;
    }
    x_magic_set(sem, s);
    return 0;
}

int k_sem_destroy(k_sem_t *sem)
{
    if (!x_magic_verify(sem)) {
        return -1;
    }
    sem_id s = (sem_id)x_magic_get(sem);
    if (sem_destroy(s) != 0) {
        return -1;
    }
    x_magic_reset(sem);
    return 0;
}

int k_sem_timedwait(k_sem_t *sem, uint32_t timeout)
{
    if (!x_magic_verify(sem)) {
        return -1;
    }
    return sem_timedwait((sem_id)x_magic_get(sem), timeout);
}

int k_sem_trywait(k_sem_t *sem)
{
    if (!x_magic_verify(sem)) {
        return -1;
    }
    return sem_trywait((sem_id)x_magic_get(sem));
}

int k_sem_wait(k_sem_t *sem)
{
    if (!x_magic_verify(sem)) {
        return -1;
    }
    return sem_wait((sem_id)x_magic_get(sem));
}

int k_sem_post(k_sem_t *sem)
{
    if (!x_magic_verify(sem)) {
        return -1;
    }
    return sem_post((sem_id)x_magic_get(sem));
}

#ifdef CONFIG_IPC_EVENT
int k_event_init(k_event_t *event)
{
    if (x_magic_verify(event)) {
        return -1;
    }
    event_id e = event_create();
    if (e == NULL) {
        return -1;
    }
    x_magic_set(event, e);
    return 0;
}

int k_event_destroy(k_event_t *event)
{
    if (!x_magic_verify(event)) {
        return -1;
    }
    event_id e = (event_id)x_magic_get(event);
    if (event_destroy(e) != 0) {
        return -1;
    }
    x_magic_reset(event);
    return 0;
}

int k_event_timedrecv(k_event_t *event, uint32_t wanted, uint32_t option, uint32_t timeout,
                      uint32_t *recved)
{
    if (!x_magic_verify(event)) {
        return -1;
    }
    event_id e = (event_id)x_magic_get(event);
    if (event_timedrecv(e, wanted, option, timeout, recved) != 0) {
        return -1;
    }
    return 0;
}

int k_event_tryrecv(k_event_t *event, uint32_t wanted, uint32_t option, uint32_t *recved)
{
    return k_event_timedrecv(event, wanted, option, NO_WAIT, recved);
}

int k_event_recv(k_event_t *event, uint32_t wanted, uint32_t option, uint32_t *recved)
{
    return k_event_timedrecv(event, wanted, option, WAIT_FOREVER, recved);
}

int k_event_send(k_event_t *event, uint32_t value)
{
    if (!x_magic_verify(event)) {
        return -1;
    }
    event_id e = (event_id)x_magic_get(event);
    if (event_send(e, value) != 0) {
        return -1;
    }
    return 0;
}
#else
int k_event_init(k_event_t *event)
{
    return -ENOSYS;
}

int k_event_destroy(k_event_t *event)
{
    return -ENOSYS;
}

int k_event_timedrecv(k_event_t *event, uint32_t wanted, uint32_t option, uint32_t timeout,
                      uint32_t *recved)
{
    return -ENOSYS;
}

int k_event_tryrecv(k_event_t *event, uint32_t wanted, uint32_t option, uint32_t *recved)
{
    return -ENOSYS;
}

int k_event_recv(k_event_t *event, uint32_t wanted, uint32_t option, uint32_t *recved)
{
    return -ENOSYS;
}

int k_event_send(k_event_t *event, uint32_t value)
{
    return -ENOSYS;
}
#endif

int k_event_clear(k_event_t *event, uint32_t value)
{
    return -ENOSYS;
}

#ifdef CONFIG_IPC_MQ
int k_mq_create(k_mq_t *mq, const char *name, size_t msg_size, size_t max_msgs, uint32_t options)
{
    if (x_magic_verify(mq)) {
        return -1;
    }
    mq_id m = mq_create(msg_size, max_msgs, options);
    if (m == NULL) {
        return -1;
    }
    x_magic_set(mq, m);
    return 0;
}

int k_mq_delete(k_mq_t *mq)
{
    if (!x_magic_verify(mq)) {
        return -1;
    }
    mq_id m = (mq_id)x_magic_get(mq);
    if (mq_delete(m) != 0) {
        return -1;
    }
    x_magic_reset(mq);
    return 0;
}

int k_mq_timedsend(k_mq_t *mq, const void *buf, size_t len, uint32_t timeout)
{
    if (!x_magic_verify(mq)) {
        return -1;
    }
    mq_id m = (mq_id)x_magic_get(mq);
    if (mq_timedsend(m, (void *)buf, len, timeout) != 0) {
        return -1;
    }
    return 0;
}

int k_mq_trysend(k_mq_t *mq, const void *buf, size_t len)
{
    return k_mq_timedsend(mq, buf, len, NO_WAIT);
}

int k_mq_send(k_mq_t *mq, const void *buf, size_t len)
{
    return k_mq_timedsend(mq, buf, len, WAIT_FOREVER);
}

int k_mq_timedrecv(k_mq_t *mq, void *buf, size_t len, uint32_t timeout)
{
    if (!x_magic_verify(mq)) {
        return -1;
    }
    mq_id m = (mq_id)x_magic_get(mq);
    if (mq_timedrecv(m, buf, len, timeout) != 0) {
        return -1;
    }
    return 0;
}

int k_mq_tryrecv(k_mq_t *mq, void *buf, size_t len)
{
    return k_mq_timedrecv(mq, buf, len, NO_WAIT);
}

int k_mq_recv(k_mq_t *mq, void *buf, size_t len)
{
    return k_mq_timedrecv(mq, buf, len, WAIT_FOREVER);
}

#else
int k_mq_create(k_mq_t *mq, const char *name, size_t msg_size, size_t max_msgs, uint32_t options)
{
    return -ENOSYS;
}

int k_mq_delete(k_mq_t *mq)
{
    return -ENOSYS;
}

int k_mq_timedsend(k_mq_t *mq, const void *buf, size_t len, uint32_t timeout)
{
    return -ENOSYS;
}

int k_mq_trysend(k_mq_t *mq, const void *buf, size_t len)
{
    return -ENOSYS;
}

int k_mq_send(k_mq_t *mq, const void *buf, size_t len)
{
    return -ENOSYS;
}

int k_mq_timedrecv(k_mq_t *mq, void *buf, size_t len, uint32_t timeout)
{
    return -ENOSYS;
}

int k_mq_tryrecv(k_mq_t *mq, void *buf, size_t len)
{
    return -ENOSYS;
}

int k_mq_recv(k_mq_t *mq, void *buf, size_t len)
{
    return -ENOSYS;
}
#endif   // CONFIG_IPC_MQ

#ifdef CONFIG_SOFT_TIMER
int k_timer_create(k_timer_t *timer, uint32_t interval, uint16_t mode, void (*pfn)(uintptr_t arg),
                   uintptr_t arg)
{
    if (x_magic_verify(timer)) {
        return -1;
    }
    uint16_t flags;

    switch (mode) {
    case K_TIMER_MODE_ONESHOT:
        flags = TIMER_FLAG_ONE_SHOT;
        break;
    default:
        flags = TIMER_FLAG_REPEATED;
        break;
    }

    timer_id t = timer_create(flags, interval, pfn, arg);
    if (t == NULL) {
        return -1;
    }
    x_magic_set(timer, t);
    return 0;
}

int k_timer_delete(k_timer_t *timer)
{
    if (!x_magic_verify(timer)) {
        return -1;
    }
    timer_id t = (timer_id)x_magic_get(timer);
    if (timer_delete(t) != 0) {
        return -1;
    }
    x_magic_reset(timer);
    return 0;
}

int k_timer_start(k_timer_t *timer)
{
    if (!x_magic_verify(timer)) {
        return -1;
    }
    timer_id t = (timer_id)x_magic_get(timer);
    if (timer_start(t) != 0) {
        return -1;
    }
    return 0;
}

int k_timer_stop(k_timer_t *timer)
{
    if (!x_magic_verify(timer)) {
        return -1;
    }
    timer_id t = (timer_id)x_magic_get(timer);
    if (timer_stop(t) != 0) {
        return -1;
    }
    return 0;
}
#else
int k_timer_create(k_timer_t *timer, uint32_t interval, uint16_t mode, void (*pfn)(uintptr_t arg),
                   uintptr_t arg)
{
    return -ENOSYS;
}

int k_timer_delete(k_timer_t *timer)
{
    return -ENOSYS;
}

int k_timer_start(k_timer_t *timer)
{
    return -ENOSYS;
}

int k_timer_stop(k_timer_t *timer)
{
    return -ENOSYS;
}
#endif

uint64_t k_mseconds(void)
{
    return tick_ms();
}

uint32_t k_tick2ms(uint32_t ticks)
{
    return tick_tick2ms(ticks);
}

uint32_t k_ms2tick(uint32_t ms)
{
    return tick_ms2tick(ms);
}
